package org.hxyjs.service.impl;


import org.hxyjs.dao.RegionDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Region;
import org.hxyjs.entity.Region;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.RegionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("RegionService")
public class RegionServiceImpl implements RegionService {
    @Resource
    RegionDao regionDao;
    @Override
    public DTO<Region> getAllRegions()  throws Exception{
        List<Region> regions = regionDao.getAllRegions();
        MessageEnum messageEnum  = Optional.ofNullable(regions).isPresent()?MessageEnum.GETREGIONSUCCESS: MessageEnum.GETREGIONFAIL;
        return new DTO<Region>(messageEnum.getCode(),messageEnum.getMessage(),regions);
    }

    @Override
    public DTO<Region> deleteRegionByRno(String rno)  throws Exception{
        Integer result = regionDao.deleteRegionByRno(rno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEREGIONSUCCESS:MessageEnum.DELETEREGIONFAIL;
        return new DTO<Region>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Region> updateRegionByRno(Region region)  throws Exception{
        Integer result = regionDao.updateRegionByRno(region);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEREGIONSUCCESS:MessageEnum.UPDATEREGIONFAIL;
        return new DTO<Region>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Region> addRegion(Region region) throws Exception {
        Integer result = regionDao.addRegion(region);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDREGIONSUCCESS:MessageEnum.ADDREGIONFAIL;
        return new DTO<Region>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Region> getRegionByPage(PageInfo pageInfo ) throws Exception {
        List<Region> regions = regionDao.getRegionByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(regions).isPresent()?MessageEnum.GETREGIONSUCCESS: MessageEnum.GETREGIONFAIL;
        return new DTO<Region>(messageEnum.getCode(),messageEnum.getMessage(),regions,pageInfo);
    }
}
